package com.sqpoll.android.group;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.sqpoll.android.R;
import com.sqpoll.android.member.Member;
import com.sqpoll.android.member.MemberListAdapter;

/**
 * The activity for group member list.
 * 
 * @author Younggue Bae
 */
public class GroupMemberListActivity extends SherlockFragmentActivity {

	public static final String TAG = "GroupMemberListActivity";

	LayoutInflater mInflater;
	private ListView mListView;
	private MemberListAdapter mAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_group_member_list);
		
		Intent intent = getIntent();
		String groupName = intent.getStringExtra("group.name");

		ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayShowTitleEnabled(true);
		actionBar.setTitle(groupName);
		actionBar.setHomeButtonEnabled(true);
		actionBar.setDisplayHomeAsUpEnabled(true);

		List<Member> items = loadItems();
		mAdapter = new MemberListAdapter(this, R.layout.list_item_member, items);
		mListView = (ListView) findViewById(R.id.list_view);
		
		mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		mListView.setOnItemClickListener(mItemClickListener);
		mListView.setAdapter(mAdapter);
	}

	OnItemClickListener mItemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
			Member currentItem = (Member) mAdapter.getItem(position);
			String name = currentItem.getName();
			Toast.makeText(getBaseContext(), "Selected : " + name, Toast.LENGTH_SHORT).show();
		}
	};
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);

		switch (item.getItemId()) {
		case android.R.id.home:
			onBackPressed();
			// Intent intent = new Intent(this, MainActivity.class);
			// startActivity(intent);
			break;
		}

		return true;
	}

	private List<Member> loadItems() {
		Resources res = getResources();

		List<Member> items = new ArrayList<Member>();

		Member item = new Member();
		item.setProfileImage(res.getDrawable(R.drawable.ic_co_profile_sample));
		item.setName("Sonya Cho");
		
		items.add(item);

		item = new Member();
		item.setProfileImage(res.getDrawable(R.drawable.ic_co_profile_sample));
		item.setName("Younggue Bae");
		
		items.add(item);
		
		item = new Member();
		item.setProfileImage(res.getDrawable(R.drawable.ic_co_profile_sample));
		item.setName("Honggyun Bae");
		
		items.add(item);

		item = new Member();
		item.setProfileImage(res.getDrawable(R.drawable.ic_co_profile_sample));
		item.setName("Kyoungwoo Bae");
		
		items.add(item);

		item = new Member();
		item.setProfileImage(res.getDrawable(R.drawable.ic_co_profile_sample));
		item.setName("Minseo Bae");
		
		items.add(item);

		return items;
	}

}